﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Documents;
using System.Windows.Media;

namespace DuSQL.WpfControls
{ 
    public class SqlParagraphProcessor : IParagraphProcessor
    {
        private static readonly Regex _splitRegex = new Regex(@"(\s|\(|\)|\+|\-|\%|\*|\[|\]|/)", RegexOptions.Compiled);
        #region Keywords
        private static readonly HashSet<string> _keyWords =
            new HashSet<string>(StringComparer.CurrentCultureIgnoreCase)
                {
                    "ADD",
                    "ALL",
                    "ALTER",
                    "AND",
                    "ANY",
                    "AS",
                    "ASC",
                    "AUTHORIZATION",
                    "BACKUP",
                    "BEGIN",
                    "BETWEEN",
                    "BREAK",
                    "BROWSE",
                    "BULK",
                    "BY",
                    "CASCADE",
                    "CASE",
                    "CHECK",
                    "CHECKPOINT",
                    "CLOSE",
                    "CLUSTERED",
                    "COALESCE",
                    "COLUMN",
                    "COMMIT",
                    "COMMITTED",
                    "COMPUTE",
                    "CONFIRM",
                    "CONSTRAINT",
                    "CONTAINS",
                    "CONTAINSTABLE",
                    "CONTINUE",
                    "CONTROLROW",
                    "CONVERT",
                    "CREATE",
                    "CROSS",
                    "CURRENT",
                    "CURRENT_DATE",
                    "CURRENT_TIME",
                    "CURRENT_TIMESTAMP",
                    "CURRENT_USER",
                    "CURSOR",
                    "DATABASE",
                    "DBCC",
                    "DEALLOCATE",
                    "DECLARE",
                    "DEFAULT",
                    "DELETE",
                    "DENY",
                    "DESC",
                    "DISK",
                    "DISTINCT",
                    "DISTRIBUTED",
                    "DOUBLE",
                    "DROP",
                    "DUMMY",
                    "DUMP",
                    "ELSE",
                    "END",
                    "ERRLVL",
                    "ERROREXIT",
                    "ESCAPE",
                    "EXCEPT",
                    "EXEC",
                    "EXECUTE",
                    "EXISTS",
                    "EXIT",
                    "FETCH",
                    "FILE",
                    "FILLFACTOR",
                    "FLOPPY",
                    "FOR",
                    "FOREIGN",
                    "FREETEXT",
                    "FREETEXTTABLE",
                    "FROM",
                    "FULL",
                    "GOTO",
                    "GRANT",
                    "GROUP",
                    "HAVING",
                    "HOLDLOCK",
                    "IDENTITY",
                    "IDENTITY_INSERT",
                    "IDENTITYCOL",
                    "IF",
                    "IN",
                    "INDEX",
                    "INNER",
                    "INSERT",
                    "INTERSECT",
                    "INTO",
                    "IS",
                    "ISOLATION",
                    "JOIN",
                    "KEY",
                    "KILL",
                    "LEFT",
                    "LEVEL",
                    "LIKE",
                    "LINENO",
                    "LOAD",
                    "MAX",
                    "MIN",
                    "MIRROREXIT",
                    "NATIONAL",
                    "NOCHECK",
                    "NONCLUSTERED",
                    "NOT",
                    "NULL",
                    "NULLIF",
                    "OF",
                    "OFF",
                    "OFFSETS",
                    "ON",
                    "ONCE",
                    "ONLY",
                    "OPEN",
                    "OPENDATASOURCE",
                    "OPENQUERY",
                    "OPENROWSET",
                    "OPTION",
                    "OR",
                    "ORDER",
                    "OUTER",
                    "OVER",
                    "PERCENT",
                    "PERM",
                    "PERMANENT",
                    "PIPE",
                    "PLAN",
                    "PRECISION",
                    "PREPARE",
                    "PRIMARY",
                    "PRINT",
                    "PRIVILEGES",
                    "PROC",
                    "PROCEDURE",
                    "PROCESSEXIT",
                    "PUBLIC",
                    "RAISERROR",
                    "READ",
                    "READTEXT",
                    "RECONFIGURE",
                    "REFERENCES",
                    "REPEATABLE",
                    "REPLICATION",
                    "RESTORE",
                    "RESTRICT",
                    "RETURN",
                    "REVOKE",
                    "RIGHT",
                    "ROLLBACK",
                    "ROWCOUNT",
                    "ROWGUIDCOL",
                    "RULE",
                    "SAVE",
                    "SCHEMA",
                    "SELECT",
                    "SERIALIZABLE",
                    "SESSION_USER",
                    "SET",
                    "SETUSER",
                    "SHUTDOWN",
                    "SOME",
                    "STATISTICS",
                    "SYSTEM_USER",
                    "TABLE",
                    "TAPE",
                    "TEMP",
                    "TEMPORARY",
                    "TEXTSIZE",
                    "THEN",
                    "TO",
                    "TOP",
                    "TRAN",
                    "TRANSACTION",
                    "TRIGGER",
                    "TRUNCATE",
                    "TSEQUAL",
                    "UNCOMMITTED",
                    "UNION",
                    "UNIQUE",
                    "UPDATE",
                    "UPDATETEXT",
                    "USE",
                    "USER",
                    "VALUES",
                    "VARYING",
                    "VIEW",
                    "WAITFOR",
                    "WHEN",
                    "WHERE",
                    "WHILE",
                    "WITH",
                    "WORK",
                    "WRITETEXT",
                    "CHAR",
                    "NCHAR",
                    "VARCHAR",
                    "NVARCHAR",
                    "UNIQUEIDENTIFIER",
                    "INT",
                    "BIT",
                    "DATETIME",
                    "SMALLDATETIME",
                    "TEXT",
                    "NTEXT",
                    "FLOAT",
                    "REAL",
                    "DECIMAL",
                    "NUMERIC",
                    "MONEY",
                    "SMALLMONEY",
                    "TIMESTAMP",
                    "BINARY",
                    "VARBINARY",
                    "IMAGE"    
                };
        #endregion

        #region Functions
        private static readonly HashSet<string> _functions =
            new HashSet<string>(StringComparer.CurrentCultureIgnoreCase)
                {
                    "@@CONNECTIONS",
                    "@@DATEFIRST",
                    "@@DBTS",
                    "@@LANGUAGE",
                    "@@LANGID",
                    "@@LOCK_TIMEOUT",
                    "@@MAX_CONNECTIONS",
                    "@@MAX_PRECISION",
                    "@@OPTIONS",
                    "@@NESTLEVEL",
                    "@@REMSERVER",
                    "@@SPID",
                    "@@SERVERNAME",
                    "@@SERVICENAME",
                    "@@TEXTSIZE",
                    "@@VERSION",
                    "@@CURSOR_ROWS",
                    "COUNT",
                    "SUM",
                    "AVG",
                    "CURSOR_STATUS",
                    "@@FETCH_STATUS",
                    "DATEADD",
                    "DATEDIFF",
                    "DATENAME",
                    "DATEPART",
                    "DAY",
                    "GETDATE",
                    "MONTH",
                    "YEAR",
                    "ABS",
                    "ACOS",
                    "ASIN",
                    "ATAN",
                    "ATN2",
                    "CEILING",
                    "COS",
                    "COT",
                    "DEGREES",
                    "EXP",
                    "FLOOR",
                    "LOG",
                    "LOG10",
                    "PI",
                    "POWER",
                    "RADIANS",
                    "RAND",
                    "ROUND",
                    "SIGN",
                    "SIN",
                    "SQUARE",
                    "SQRT",
                    "TAN",
                    "COL_LENGTH",
                    "COL_NAME",
                    "COLUMNPROPERTY",
                    "DATABASEPROPERTY",
                    "DB_ID",
                    "DB_NAME",
                    "FILE_ID",
                    "FILE_NAME",
                    "FILEGROUP_ID",
                    "FILEGROUP_NAME",
                    "FILEGROUPPROPERTY",
                    "FILEPROPERTY",
                    "FULLTEXTCATALOGPROPERTY",
                    "FULLTEXTSERVICEPROPERTY",
                    "INDEX_COL",
                    "INDEXPROPERTY",
                    "OBJECT_ID",
                    "OBJECT_NAME",
                    "OBJECTPROPERTY",
                    "@@PROCID",
                    "TYPEPROPERTY",
                    "IS_MEMBER",
                    "IS_SRVROLEMEMBER",
                    "SUSER_ID",
                    "SUSER_NAME",
                    "SUSER_SID",
                    "SUSER_SNAME",
                    "USER_ID",
                    "USER",
                    "ASCII",
                    "CHAR",
                    "CHARINDEX",
                    "DIFFERENCE",
                    "LEFT",
                    "LEN",
                    "LOWER",
                    "LTRIM",
                    "NCHAR",
                    "PATINDEX",
                    "REPLACE",
                    "QUOTENAME",
                    "REPLICATE",
                    "REVERSE",
                    "RIGHT",
                    "RTRIM",
                    "SOUNDEX",
                    "SPACE",
                    "STR",
                    "STUFF",
                    "SUBSTRING",
                    "UNICODE",
                    "UPPER",
                    "APP_NAME",
                    "CASE",
                    "CAST",
                    "CONVERT",
                    "COALESCE",
                    "CURRENT_TIMESTAMP",
                    "CURRENT_USER",
                    "DATALENGTH",
                    "@@ERROR",
                    "FORMATMESSAGE",
                    "GETANSINULL",
                    "HOST_ID",
                    "HOST_NAME",
                    "IDENT_INCR",
                    "IDENT_SEED",
                    "@@IDENTITY",
                    "IDENTITY (Function)",
                    "ISDATE",
                    "ISNULL",
                    "ISNUMERIC",
                    "NEWID",
                    "NULLIF",
                    "PARSENAME",
                    "PERMISSIONS",
                    "@@ROWCOUNT",
                    "SESSION_USER",
                    "STATS_DATE",
                    "SYSTEM_USER",
                    "@@TRANCOUNT",
                    "USER_NAME",
                    "@@CPU_BUSY",
                    "@@IDLE",
                    "@@IO_BUSY",
                    "@@PACK_RECEIVED",
                    "@@PACK_SENT",
                    "@@PACKET_ERRORS",
                    "@@TIMETICKS",
                    "@@TOTAL_ERRORS",
                    "@@TOTAL_READ",
                    "PATINDEX",
                    "TEXTPTR",
                    "TEXTVALID"
                };
        #endregion


        public Regex SplitWordsRegex { get { return _splitRegex; } }
        public int GetWordTypeID(string word)
        {
            if (_keyWords.Contains(word))
            {
                return 1;
            }
            if (_functions.Contains(word))
            {
                return 2;
            }
            if (word.StartsWith("@"))
            {
                return 3;
            }
            return 0;
        }
        public Inline FormatInlineForID(Inline inline, int id)
        {
            if (id == 1)
            {
                inline.Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x00, 0xFF));
            }
            else if (id == 2)
            {
                inline.Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0x00, 0xFF));
            }
            else if (id == 3)
            {
                inline.Foreground = new SolidColorBrush(Color.FromArgb(0xFF, 0x33, 0x99, 0xCC));
            }
            return inline;
        }

    }
}